package com.gxa.modules.app.outpatient.medicalrecord.controller;

import com.gxa.common.utils.Result;
import com.gxa.modules.app.outpatient.medicalrecord.service.MedicalRecordMailingService;
import com.gxa.modules.model.dto.InpatientDto;
import com.gxa.modules.model.entity.hospital.MedicalRecordMailing;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * 作者 yuanqiufeng
 * Date 2022/11/14
 */
@Api(tags = "病历寄送")
@RestController
@RequestMapping("/medicalrecod-mailing")
public class MedicalRecordMailingController {
    @Autowired
    private MedicalRecordMailingService medicalRecordMailingService;

    @InitBinder
    protected void initBinder(WebDataBinder binder) {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));
    }

    @ApiOperation(value = "按日期和账户ID查询病历寄送情况")
    @GetMapping("/list/{accountId}/{startDate}/{endDate}")
    @ApiResponses({
            @ApiResponse(code = 0,message = "success",response = MedicalRecordMailing.class)
    })
    public Result list(@PathVariable("accountId") Integer accountId, @PathVariable("startDate") Date startDate, @PathVariable("endDate") Date endDate){
        try {
            System.out.println(startDate);
            List<MedicalRecordMailing> medicalRecordMailingList = this.medicalRecordMailingService.queryAll(accountId, startDate,endDate);
            return new Result().ok(medicalRecordMailingList);
        }catch (Exception e){
            return new Result().error("查询失败");
        }
    }

    @ApiOperation(value = "根据填写的住院号查找到对应信息")
    @GetMapping("/query")
    @ApiResponse(code = 0,message = "success",response = InpatientDto.class)
    public Result query(@RequestParam String inpatientNum,@RequestParam String hospitalizedPeople){
        try {
            InpatientDto inpatientDto = this.medicalRecordMailingService.queryInpatientDetailById(inpatientNum);
            if(inpatientDto==null || !inpatientDto.getName().equals(hospitalizedPeople)){
                return new Result().error("没有此人，请检查是否输入正确");
            }
            return new Result().ok(inpatientDto);
        }catch (Exception e){
            return new Result().error("查询失败");
        }
    }

    @ApiOperation(value = "添加病案寄送")
    @PostMapping("/add")
    public Result addMedicalRecordMailing(@RequestBody MedicalRecordMailing medicalRecordMailing){
        try {

            long l = System.currentTimeMillis();
            Date date = new Date(l);
            medicalRecordMailing.setApplicationDate(date);
            System.out.println(medicalRecordMailing.toString());
            boolean save = medicalRecordMailingService.save(medicalRecordMailing);
            return new Result().ok("添加成功");
        }catch (Exception e){
            e.printStackTrace();
            return new Result().error("添加失败");
        }
    }

    @ApiOperation(value = "根据病历寄送的id查询病历寄送详细情况")
    @GetMapping("/list/{id}")
    @ApiResponse(code = 0,message = "success",response = MedicalRecordMailing.class)
    public Result queryById(@PathVariable("id") Integer id){
        try {
            MedicalRecordMailing medicalRecordMailing = this.medicalRecordMailingService.queryById(id);
            return new Result().ok(medicalRecordMailing);
        }catch (Exception e){
            e.printStackTrace();
            return new Result().error("查询失败");
        }
    }

}
